package com.geek.jdbc.work2v2.dao;

import com.geek.jdbc.work2v2.entity.Customer;
import com.geek.jdbc.work2v2.pool.ThreadLocalByC3P0Utils;

import java.sql.SQLException;
import java.util.List;


public class CustomerDao extends BaseDao {
    //根据名字模糊查询
    public List<Customer> selectAllStudent(String name) throws SQLException {
//        String sql1 = "select * from girl where name=? and phone=?";
        String sql = "select * from customers where name like ?";

        List<Customer> list = this.selectAll(
                ThreadLocalByC3P0Utils.getConnection(),
                sql, "%" + name + "%");

        return list;
    }

    //精确查询方法
    public Customer selectStudentByName(String name) throws SQLException {
        String sql = "select * from customers where name = ?";
        return this.selectOne(ThreadLocalByC3P0Utils.getConnection(), sql, name);
    }

    //根据编号查询信息
    public Customer selectStudentById(String id) throws SQLException {
        String sql = "select * from customers where id = ?";
        return this.selectOne(ThreadLocalByC3P0Utils.getConnection(), sql, id);
    }

    //插入学生信息
    public int insertStudent(Customer stu) throws SQLException {
        String sql = "insert into customers values(?, ?, ?, ?)";
        return this.update(ThreadLocalByC3P0Utils.getConnection(), sql, stu.getId(), stu.getName(),
                stu.getEmail(), stu.getBrithday());
    }


    //修改日期
    public boolean updateByDate(String id, String date) throws SQLException {
        String sql = "update customers set birthday = ? where id = ?";
        int i = this.update(ThreadLocalByC3P0Utils.getConnection(), sql, date, id);
        if (i == 1){
            return true;
        }
        return false;
    }

    //修改日期
    public boolean updateByName(String id, String name) throws SQLException {
        String sql = "update customers set name = ? where id = ?";
        int i = this.update(ThreadLocalByC3P0Utils.getConnection(), sql, name, id);
        if (i == 1){
            return true;
        }
        return false;
    }

    //删除信息
    public boolean delete(String id) throws SQLException {
        String sql = "delete from customers where id = ?";
        int i = this.update(ThreadLocalByC3P0Utils.getConnection(), sql, id);
        if (i > 0){
            return true;
        }
        return false;
    }

    //查询所有信息
    public List<Customer> selectAllList() throws SQLException {
        String sql = "select * from customers";
        List<Customer> list = this.selectAll(ThreadLocalByC3P0Utils.getConnection(), sql, null);
        return list;
    }
}
